package com.zjbti.notepad;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import org.jetbrains.annotations.NotNull;

import java.util.List;

import cn.itcast.notepad.bean.NoteBean;

public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.MyViewHolder> {
    private List<NoteBean> list;
    private Context mContext;
    private ItemClickListener itemClickListener;
    NoteAdapter(Context context, ItemClickListener itemClickListener){
        this.itemClickListener=itemClickListener;
        this.mContext=context;
    }
    public void setData(List<NoteBean> list) {
         this.list=list;
         notifyDataSetChanged();
    }
    @NonNull
    @NotNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
        View itemView= LayoutInflater.from(mContext).inflate(R.layout.list_item,parent,false);
        MyViewHolder holder=new MyViewHolder(itemView);

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull @NotNull MyViewHolder holder, int position) {
        View itemView=holder.itemView;
         NoteBean bean=list.get(position);
         if (bean==null)return;
         holder.tv_title.setText(bean.getTitle());
         holder.tv_content.setText(bean.getContent());
         holder.tv_time.setText(bean.getTime());
         itemView.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 itemClickListener.onItemClick(itemView,position);
             }
         });
         itemView.setOnLongClickListener(new View.OnLongClickListener() {
             @Override
             public boolean onLongClick(View v) {
                 itemClickListener.onItemLongClick(itemView,position);
                 return true;
             }
         });
    }

    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder {
        TextView tv_title,tv_content, tv_time;

        public MyViewHolder(@NonNull @NotNull View view) {
            super(view);
            tv_title=view.findViewById(R.id.tv_title);
            tv_content=view.findViewById(R.id.tv_content);
            tv_time=view.findViewById(R.id.tv_time);
        }
    }
    public interface ItemClickListener {
    void onItemClick(View v, int position);
    void onItemLongClick(View v, int position);
  }
}
